package com.elsa.smart_buffet.controller;

import com.alibaba.fastjson.JSON;
import com.elsa.smart_buffet.mapper.ReportMapper;
import com.elsa.smart_buffet.pojo.Evaluate;
import com.elsa.smart_buffet.pojo.Feedback;
import com.elsa.smart_buffet.pojo.Report;
import com.elsa.smart_buffet.pojo.ResultBox.ResponseResult;
import com.elsa.smart_buffet.service.FeedbackService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

@RestController
@CrossOrigin
@Slf4j
public class FeedbackController {

    @Autowired
    private FeedbackService feedbackService;

    @Autowired
    private ReportMapper reportMapper;

    @GetMapping("/feedback/list")
    public ResponseResult selectAllFeedback() {
        return feedbackService.selectAllFeedback();
    }

    @GetMapping("/feedback/{id}")
    public ResponseResult selectOneByIdFeedback(@PathVariable("id") int id) {
        return feedbackService.selectOneByIdFeedback(id);
    }

    @PostMapping("/updateFeedback")
    public ResponseResult updateFeedback(@RequestBody String data) {
        Map<String, Object> map = (Map<String, Object>) JSON.parseObject(data).get("data");
//        System.out.println(map);
        Report report = new Report();
        report.setRId((Integer) map.get("rId"));
        report.setCId((Integer) map.get("consumerId"));
        report.setFStatus(1);
        report.setFComplaint((String) map.get("feedbackComplaint"));
        report.setFTime((String) map.get("feedbackTime"));
        Date date = new Date();
//        SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
//        System.out.println(formatter.format(date));
        report.setFmTime(DateFormatUtils.format(date,"yyyy-MM-dd HH:mm:ss"));
        report.setFmOpinion((String) map.get("returnOpinion"));
        log.info(String.valueOf(report));
//        return new ResponseResult(200, null);
        return feedbackService.updateFeedback(report);
    }

    @DeleteMapping("/feedback/{id}")
    public ResponseResult deleteByIdFeedback(@PathVariable int id) {
        return  feedbackService.deleteByIdFeedback(id);
    }

    @PostMapping("/feedback")
    public ResponseResult insertFeedback(@RequestBody String data) {
//        Feedback feedback1 = new Feedback();
        Report report = new Report();
        Map<String, Object> data1 = (Map<String, Object>) JSON.parseObject(data).get("data");
        log.info(String.valueOf(data1));
        String cId = data1.get("cId").toString();
        report.setCId(Integer.parseInt(cId));
        String feedback = data1.get("feedback").toString();
        report.setFComplaint(feedback);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        java.sql.Date ctime2 = new java.sql.Date(new java.util.Date().getTime());
        report.setFTime(df.format(ctime2));
        report.setFStatus(0);
        report.setRId(reportMapper.selectCount(null) + 1);
        return feedbackService.insertFeedback(report);
    }

    @PostMapping("/selectFeedbackInOrderByTime")
    public ResponseResult selectFeedbackInOrderByTime(@RequestBody String time) {
        return feedbackService.selectFeedbackInOrderByTime(JSON.parseObject(time).get("time").toString());
    }

    @PostMapping("/selectFeedbackBySearchData")
    public ResponseResult selectEvaluateBySearchData(@RequestBody String data) {
        return feedbackService.selectFeedbackBySearchData(JSON.parseObject(data).get("data").toString());
    }

    @PostMapping("/selectReportByCIdAndStatus")
    public ResponseResult selectReportByCIdAndStatus(@RequestBody String data) {
        return feedbackService.selectReportByCIdAndStatus(JSON.parseObject(data).get("data").toString());
    }

}
